'''
https://leetcode.cn/problems/find-peak-element/description/
'''
import time
from typing import List
import matplotlib.pyplot as plt
import numpy as np


class Solution:
    def findPeakElement(self, nums: List[int]) -> int:
        if not nums: return -1
        if len(nums) == 1: return 0
        if nums[0] > nums[1]: return 0
        if nums[-1] > nums[-2]: return len(nums) - 1
        l, r = 0, len(nums) - 1
        while l <= r:
            m = (l + r) // 2
            if nums[m] > nums[m-1] and nums[m] > nums[m+1]: return m
            else:
                if nums[m-1] > nums[m]:
                    r = m - 1
                elif nums[m+1] > nums[m]:
                    l = m + 1
        return -1

def draw_peak(data: List[int]) -> None:
    plt.plot(data)
    plt.xlabel('Index')
    plt.ylabel('Value')
    plt.show()

nums = [1,2,1,3,5,6,4]
start_time = time.time()
draw_peak(nums)
execution_time = time.time() - start_time
print(f'绘制图形耗时: {execution_time}s')
print(f'绘制图形耗时: {execution_time * 1000}ms')
print(f'绘制图形耗时: {execution_time * 1e9}ns')
print(Solution().findPeakElement(nums))